<?php
namespace app\admin\controller;

class Article extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['keyword']!=''){ $map[] = ['and','a.title','like',"%{$d['keyword']}%"]; }

            //if($d['cat_id']!=''){ $map[] = ['and','a.cat_id','=',$d['cat_id']]; }

            if($d['rec_id']!=''){ $map[] = ['and','a.rec_id','like',"%,{$d['rec_id']},%"]; }
            if($d['min_time']!='') $map[] = ['and','a.create_time','>=',strtotime($d['min_time'])];
            if($d['max_time']!='') $map[] = ['and','a.create_time','<=',strtotime($d['max_time'])];
            $w  = where($map);
            if($d['cat_id']!=''){
                $ids = m('ArticleCat','c')->getAllchildCat($d['cat_id']);
                if($ids){
                    $ids = join(',',$ids);
                }else{
                    $ids = $d['cat_id'];
                }
                if($w[0]){
                    $w[0] .= " and a.cat_id in ({$ids})";
                }else{
                    $w[0] = " where a.cat_id in ({$ids})";
                }

            }

            $rs = $this->db->query("select a.*,b.cat_title ptitle,c.title ctitle from article a left join article_cat b on a.cat_id = b.id left join article_rec c on a.rec_id = c.id $w[0] order by a.sort desc,a.id desc".limit($d['page'],$d['limit']), $w[1],'',true);
            if($rs){
                $rule = [
                    'status'=>function($v){return c("type.status.$v");},
                    'create_time' => 'com_time_format',
                ];
                $data = com_format($rs[0],$rule);

                //查询所有推荐位
                $rec_list = $this->db->query('select * from article_rec order by sort desc');

                foreach ($data as $key => $value) {
                    $name = "";
                    $rec_id = explode(',',$value['rec_id']);
                    foreach ($rec_id as $ke => $val) {
                        foreach ($rec_list as $k1 => $v1) {
                            if($v1['id'] == $val){
                                $name .= $v1['title'] . "|";
                            }
                        }
                    }
                    $data[$key]['ctitle'] = $name;
                }
                suc($data,$rs[1]);
            }else{
                suc([],0);
            }
            $this->fetch();
        }
        $rec = $this->db->query("select title,id from article_rec order by sort desc,id desc");
        $cat = m('ArticleCat','c')->allSubCat();
        return $this->fetch(['rec'=>$rec,'cat'=>$cat]);
    }

    //添加
    public function add(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'cat_id'=>'require',
                'title' => "require",
            ];
            $msg = [
            ];
            check($rule,$msg,$d);
            $d['create_time'] = time();
            unset($d['file']);
            if(isset($d['content'])) $d['content']=htmlspecialchars_decode($d['content']);
            $id = $this->db->insert('article',$d);
            m('Attachment','xcom')->imgSure('article',$id,$d);
            if ($id) {
                cookie('js_msg','添加成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('添加失败');
            }
        }
        $rec = $this->db->query("select title,id from article_rec order by sort desc,id desc");
        $cat = m('ArticleCat','c')->allSubCat();
        return $this->fetch(['rec'=>$rec,'cat'=>$cat]);
    }

    //编辑
    public function edit(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'id'=>'require',
                'cat_id'=>'require',
                'title' => "require",
            ];
            $msg = [
            ];
            check($rule,$msg,$d);
            $d['update_time'] = time();
            unset($d['file']);
            if(isset($d['content'])) $d['content']=htmlspecialchars_decode($d['content']);
            $id = $this->db->update('article',$d);
            m('Attachment','xcom')->imgSure('article',$d['id'],$d);
            if ($id) {
                cookie('js_msg','修改成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('修改失败');
            }
        }
        $rec = $this->db->query("select title,id from article_rec order by sort desc,id desc");
        $cat = m('ArticleCat','c')->allSubCat();
        $id = $this->get['id'];
        $info = $this->db->query("select * from article where id=?",[$id],1);
        return $this->fetch(['rec'=>$rec,'cat'=>$cat,'info'=>$info]);
    }
    //删除
    public function del(){
        if(is_post()){
            $d = $this->post;
            $this->db->delete('article',$d['id']);
            m('Attachment','xcom')->imgDel('article',$d['id'],true);
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }
    }

    #批量删除
    function delall(){
      if(is_post()){
            $d = $this->post;
            $ids = $d['id'];
            foreach($ids as $id){
                $this->db->delete('article',$id);
                m('Attachment','xcom')->imgDel('article',$id,true);
            }
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
      }
    }

    //详情
    public function detail($id){
        return $this->fetch();
    }

    //修改上下架
    public function updateStatus(){
      if(is_post()){
            $d = $this->post;
            $status = $this->get('status');
            $ids = $d['id'];
            foreach($ids as $id){
                $this->db->update('article',['status'=>$status],['id=?',[$id]]);
            }
            cookie('js_msg','修改成功');
            cookie('js_function','reload');
            return suc();
      }
    }

    //设置推荐位
    public function setRecommend(){
        if(is_post()){
            $d = $this->post;
            $ids = explode(',',$d['ids']);
            $rec_id = isset($d['rec_id']) ? join(',',$d['rec_id'])."," : "";
            foreach ($ids as $k => $v) {
                $data = [
                    'rec_id'=> ",0,{$rec_id}"
                ];
                $where = [
                    "id=?",
                    [$v]
                ];
                $this->db->update('article',$data,$where);
            }
            cookie('js_msg','修改成功');
            cookie('js_function','preload');
            return suc();
        }

        $ids = $this->get("ids");
        //查询所有推荐位
        $list = $this->db->query('select * from article_rec order by sort desc');
        return $this->fetch(['list'=>$list,'ids'=>join(',',$ids)]);
    }
}
